package iewShopApp.namespace;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.Spinner;

public class CountersinkActivity extends Activity {

	public GlobalVars Constants;
	public EditText plunge;
	public Spinner AngleSpinner;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.countersink_main);
		plunge = (EditText) findViewById(R.id.plungeDepth);
		Spinner AngleSpinner = (Spinner) findViewById(R.id.cntrsnkSpinner);
		plunge.setText("0");

		Constants = new GlobalVars();
		Constants.cntrsnkDia = .25;
		Constants.cntrsnkTool = 82;
		calculate();
		AngleSpinner.setSelection(1);
		final EditText cntrsnkDia = (EditText) findViewById(R.id.cntrsnkDia);
		cntrsnkDia.setText(String.valueOf(.25));

		cntrsnkDia.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press

					String strdiameter = cntrsnkDia.getText().toString();
					Constants.cntrsnkDia = Double.parseDouble(strdiameter);

					calculate();
					// Force EditText from gaining focus all the bloody time
					cntrsnkDia.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(cntrsnkDia.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);

					return true;
				}
				return false;
			}
		});

		plunge.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press

					String strdiameter = cntrsnkDia.getText().toString();

					Constants.cntrsnkDia = Double.parseDouble(strdiameter);
					calculate();

					// Force EditText from gaining focus all the bloody time
					plunge.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(plunge.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);

					return true;
				}
				return false;
			}
		});

		AngleSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener2());
	}

	public class MyOnItemSelectedListener2 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {

			String selectedTool = getResources().getStringArray(
					R.array.cntrsnk_array)[parent.getSelectedItemPosition()];
			selectedTool = selectedTool.replaceAll("[^\\d]", "");
			Constants.cntrsnkTool = Double.parseDouble(selectedTool);
			calculate();
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	void calculate() {
		plunge = (EditText) findViewById(R.id.plungeDepth);
		double plungeAmount = Math.round(1000 * Constants.cntrsnkDia
				/ (Math.tan(Math.toRadians(Constants.cntrsnkTool / 2)) * 2)) / 1000.;
		// plunge.setText(String.valueOf(plungeBlock));
		plunge.setText(String.valueOf(plungeAmount));
	}

}
